package group9;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.Key;

/**
 * @author Claudio Herreros, Dylan James, Tyler Berg
 *
 */

@SuppressWarnings("serial")
public class EstimateUserStoryServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

		String estimate = req.getParameter("estimate");
		

		PersistenceManager pm = PMF.get().getPersistenceManager();
		User user = (User) req.getSession().getAttribute("User");
		user = (User) pm.getObjectById(User.class, user.getKey());

		if (user == null)
		{
			resp.sendRedirect("login.jsp");
			return;
		}
		
		UserStory us = (UserStory) req.getSession().getAttribute("UserStory");
		us = (UserStory) pm.getObjectById(UserStory.class, us.getKey());
		
		List<Estimate> estimates = new ArrayList<Estimate>();
		
		for(int i = 0; i < estimates.size(); i++){
			if(estimates.get(i).getUserKey().equals(user.getKey())){
				estimates.get(i).setValue(Integer.parseInt(estimate));
				break;
			}
		}
		
		//close db
		pm.close();
		
		req.getSession().setAttribute("UserStory", us);
		
		//redirect
		resp.sendRedirect("/MyUserStories.jsp");
	}
}